package apka.SMSBillet.CTR;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import apka.SMSBillet.MDL.City;

public class CTRcity {
	private List<City> cityList;
	Context context;
	CTRdatabase ctrDb;
	List<City> citiesArray;
	boolean isCitiesLoaded = false;
	
	public CTRcity(Context context){
		this.context = context;
		ctrDb = new CTRdatabase(context);
		citiesArray = new ArrayList<City>();
	}
		
	private List<City> getCityList() {
		if(isCitiesLoaded == false){
			Cursor c = ctrDb.getCities();
	    	for (int i = 0; i < c.getCount(); i++) {
	    		String name = (c.getString(c.getColumnIndex("name")));
//	    		int zone = (c.getInt(c.getColumnIndex("ZoneNumber")));
//	    		int id = (c.getInt(c.getColumnIndex("_id")));
	    		City city = new City(name);
	    		citiesArray.add(city);
	    		c.moveToNext();
	    		
			}
	    	isCitiesLoaded = true;
		}
	    	else{
    		return citiesArray;
    	}
    	return citiesArray;
	}
	
	public String[] getCities(){
		List<City> objectList = getCityList();
		String[] citiesArray = new String[objectList.size()];
		
		for (int i = 0; i < objectList.size(); i++) {
			citiesArray[i] = objectList.get(i).getName();
		}
		return citiesArray;
	}
	
	public String[] getCitiesSorted(){
		return sortArray(getCities());
	}
	
    public String[] sortArray(String[] stringArray){
    	List<String> list = Arrays.asList(stringArray);
    	Collections.sort(list);
    	return stringArray;
    }
    
    public int getIdForCity(String cityString, String[] stringArray){
    	int i = 0;
    	int result = -1;
    	boolean found = false;
    	while(i < stringArray.length && found != true){
    		cityString.trim();
    		if(cityString.equals(stringArray[i])){
    			result = i;
    			found = true;
    		}
    		else{
    		}
    		i++;
    	}
    	return result;
    }
	
	
	
//    public List fillCityArray(){
//    	Cursor c = getCities();
//    	citiesArray = List<City>();
//    	for (int i = 0; i < citiesArray.length; i++) {
//    		citiesArray[i] = c.getString(c.getColumnIndex("City"));	
//    		c.moveToNext();
//		}
//    	if(citiesArray.length != 0){
//    		isCitiesLoaded = true;
//    	}
//    	return citiesArray;
//    }
	
	

}
